"""
数据库模型定义
"""
from sqlalchemy import Column, Integer, String, Text, DateTime, Float, JSON
from sqlalchemy.sql import func
from database.connection import Base


class ContentAuditRecord(Base):
    """内容审核记录表"""
    __tablename__ = 'content_audit_records'
    
    id = Column(Integer, primary_key=True, autoincrement=True, comment='审核记录ID')
    post_id = Column(Integer, nullable=False, index=True, comment='帖子ID')
    audit_result = Column(String(20), nullable=False, comment='审核结果: PASS/REJECT/MANUAL')
    confidence = Column(Float, nullable=False, comment='审核置信度')
    reasons = Column(JSON, comment='拒绝原因列表')
    details = Column(JSON, comment='详细审核信息')
    
    # 各算法的具体结果
    sensitive_words = Column(JSON, comment='敏感词检测结果')
    text_classification = Column(JSON, comment='文本分类结果')
    sentiment_analysis = Column(JSON, comment='情感分析结果')
    image_audit = Column(JSON, comment='图片审核结果')
    
    # 时间字段
    created_at = Column(DateTime, default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now(), comment='更新时间')
    
    def __repr__(self):
        return f"<ContentAuditRecord(id={self.id}, post_id={self.post_id}, result={self.audit_result})>"


class AuditStatistics(Base):
    """审核统计表"""
    __tablename__ = 'audit_statistics'
    
    id = Column(Integer, primary_key=True, autoincrement=True)
    date = Column(String(10), nullable=False, unique=True, comment='统计日期 YYYY-MM-DD')
    
    # 审核数量统计
    total_audits = Column(Integer, default=0, comment='总审核数')
    pass_count = Column(Integer, default=0, comment='通过数量')
    reject_count = Column(Integer, default=0, comment='拒绝数量')
    manual_count = Column(Integer, default=0, comment='人工审核数量')
    
    # 违规类型统计
    sensitive_words_count = Column(Integer, default=0, comment='敏感词违规数')
    spam_count = Column(Integer, default=0, comment='垃圾信息数')
    advertisement_count = Column(Integer, default=0, comment='广告推广数')
    inappropriate_count = Column(Integer, default=0, comment='不当内容数')
    image_violation_count = Column(Integer, default=0, comment='图片违规数')
    
    # 平均指标
    avg_confidence = Column(Float, default=0.0, comment='平均置信度')
    avg_processing_time = Column(Float, default=0.0, comment='平均处理时间(秒)')
    
    created_at = Column(DateTime, default=func.now())
    updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
    
    def __repr__(self):
        return f"<AuditStatistics(date={self.date}, total={self.total_audits})>"
